package me.goldze.mvvmhabit.base;

import static me.goldze.mvvmhabit.bus._NetModel.OFFLINE;
import static me.goldze.mvvmhabit.bus._NetModel.ONLINE;

import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.bus.SourceUtils;
import me.goldze.mvvmhabit.bus._NetModel;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

/**
 * Created by goldze on 2017/6/15.
 */
public abstract class BaseModel<T> implements IModel {


    public T source;
    private Disposable subscribe;

    /**
     * 共同仓库,单利创建时发个刷新源的消息，以后有离在线更改，源消息自动刷新,同时发送初始刷新请求
     */
    public BaseModel() {
        subscribe = RxBus.getDefault().toObservable(_NetModel.class)
                .subscribe(model -> {
//                    KLog.e(this.getClass().getSimpleName()+"数据仓库刷新离在线模式：" + model);
                    SourceUtils.lineMode = model;
                    refreshSource();
                });
        RxSubscriptions.add(subscribe);
        RxBus.getDefault().post(ACache.getInstance().getAsBoolean(AKey.IS_OFFLINE_MODEL) ? OFFLINE : ONLINE);
    }

    public abstract void refreshSource();

    @Override
    public void onCleared() {
        // TODO: 2023/10/18 单利也会执行，后期整改
//        RxSubscriptions.remove(subscribe);
    }
}
